currLoading = false;
interval = setInterval(function() {
        if(!currLoading) {
            run_test();
        }
    }, 50);


function run_test() {
    // create WebPage
    var page = new WebPage();
    page.settings.javascriptEnabled = false;
    page.settings.loadImages = false;
    page.loadPlugins = false;
    page.settings.localToRemoteUrlAccessEnabled = false;

    // set url to get content
    var url = "file:///home1/neo/user/yolongyi/phantomjs_test/ameblo.html";

    // load with url
    currLoading = true;
    page.open(url, function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
        } else {
            // inject wtputil javascript
            if ( !page.injectJs("wtputil.js") ) {
                console.log("wtputil.js loading failed");
            }
            
            // inject jquery javascript
/*
            var jquery_filename = "jquery.js";
            if ( !page.injectJs(jquery_filename) ) {
                console.log(jquery_filename + " load failed !!");
            }
*/

            var result = page.evaluate(function() {
                title = document.querySelector("h3.title").textContent;
                writer = document.querySelector("span.theme a").textContent;
                date = document.querySelector("span.date").textContent;
                body = document.querySelector("div.subContents").textContent;

                if(!title) {
                    title = document.querySelector("a.skinArticleTitle").textContent;
                    writer = document.querySelctor("span.articleTheme").textContent;
                    date = document.querySelctor("span.articleTime").textContent;
                    body = document.querySelctor("div.articleText").textContent;
                }
                
                var wu = new WTPUtil();
                wu.addKeyVal("_TITLE", title);
                wu.addKeyVal("_DATE", date);
                wu.addKeyVal("_WRITER", writer);
                wu.addKeyVal("_BODY", body);

                comments = document.querySelectorAll("div.each_comment");
                for (var i=0;i<comments.length;i++) {
                    var ele = comments[i];
                    commenter = ele.querySelector("p.label").textContent;
                    comment = ele.querySelector("p.comment_body").textContent;
                    commentDate = ele.querySelector("div.comment_footer span.comment_date").textContent;
                    wu.addKeyVal("_COMMENT", commenter + "|" + commentDate + "|" + comment);
                }

                tmpDiv = document.createElement("div");
                tmpDiv.innerHTML = wu.contentInfoString();
                document.documentElement.appendChild(tmpDiv);

                return wu.contentInfoString();
            });

            // end of print result
            console.log(result);

            // release
            page.release();
            currLoading = false;
            //phantom.exit();
        }
    });
}
